package com.app.core.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.android.tu.loadingdialog.LoadingDailog;
import com.app.core.config.MyApp;
import com.app.core.presenter.ParseData;
import com.app.core.presenter.ParseDataImpl;
import com.app.core.util.AppManager;
import com.app.core.util.LogUtils;
import com.app.core.util.Sputil;
import com.app.core.util.ViewFinder;
import com.app.core.util.ViewUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.specialtreasure.freightdepot.R;
import com.specialtreasure.freightdepot.mq.MQUtils;


public abstract class BaseNewActivity extends AppCompatActivity {
    public static final String TAG = "BaseNewActivity";
    //上下文
    protected Context mContext;
    //绑定这个activity布局的Finder
    protected ViewFinder finder;
    //intent
    protected Intent intent;
    //SharedPreferences工具类
    protected Sputil sp;
    //加载框
    protected LoadingDailog.Builder loadBuilder;
    protected LoadingDailog dialog;
    //网络请求
    protected ParseData parseData;
    //沉浸式状态栏
    protected ImmersionBar mImmersionBar;





    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }


    private void init() {
        mContext = MyApp.getInstance();
        finder = new ViewFinder(this);
        intent = new Intent();
        sp = MyApp.getInstance().getSp();
        parseData=new ParseDataImpl();
        //将Activity实例添加到AppManager的堆栈
        AppManager.getAppManager().addActivity(this);
        //沉浸式状态栏
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                     .statusBarColor(R.color.common_F1EFEF)
                     .navigationBarEnable(false)   //禁止对导航栏相关设置
                     .init();


        MQUtils.connectMQ(mContext);

    }


    @Override
    protected void onStart() {
        super.onStart();
        LogUtils.i(getActiviyName(), "onStart()");
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtils.i(getActiviyName(), "onResume()");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        LogUtils.i(getActiviyName(), "onRestart()");
    }

    @Override
    protected void onPause() {
        super.onPause();
        LogUtils.i(getActiviyName(), "onPause()");
    }

    @Override
    protected void onStop() {
        super.onStop();
        LogUtils.i(getActiviyName(), "onStop()");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtils.i(getActiviyName(), "onDestroy()");
        //将Activity实例从AppManager的堆栈中移除
        AppManager.getAppManager().finishActivity(this);
        //不调用该方法，如果界面bar发生改变，在不关闭app的情况下，退出此界面再进入将记忆最后一次bar改变的状态
        if (mImmersionBar != null){
            mImmersionBar.destroy();
        }

    }

    /**
     * activity name
     */
    public abstract String getActiviyName();


    /**
     * 展示View
     *
     * @param view
     * @return
     */
    protected BaseNewActivity show(final View view) {
        ViewUtils.setGone(view, false);
        return this;
    }

    /**
     * 隐藏View
     *
     * @param view
     * @return
     */
    protected BaseNewActivity hide(final View view) {
        ViewUtils.setGone(view, true);
        return this;
    }


    /**
     * 显示加载框
     *
     * @param text
     */
    public void showDialog(String text) {
        loadBuilder = new LoadingDailog.Builder(this)
                .setMessage(text)
                .setCancelable(true)
                .setCancelOutside(false);
        dialog = loadBuilder.create();

        dialog.show();


    }

    /**
     * 关闭加载框
     */
    public void closeDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }


}
